<template>
  <div class="student-management">
    <h2>学生管理</h2>

    <!-- 表格 -->
    <el-table :data="students" border style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="group_id" label="组别"></el-table-column>
      <el-table-column label="操作" width="135">
        <template #default="{ row }">
          <el-button size="small" @click="editStudent(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteStudent(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
      style="margin-top: 20px"
    />

    <!-- 弹窗：添加 / 编辑 -->
    <el-dialog v-model="dialogVisible" title="学生信息">
      <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="!formData.id">
        <el-input v-model="formData.password" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" v-else>
          <el-input v-model="formData.password" type="password" show-password placeholder="留空则不修改"></el-input>
          </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="formData.age" :min="0" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="formData.gender" placeholder="请选择">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="formData.email"></el-input>
        </el-form-item>
        <el-form-item label="组别">
          <el-input-number v-model="formData.group_id" :min="0" />
        </el-form-item>
        <el-button @click="submitStudent">提交</el-button>
      </el-form>
    </el-dialog>

    <!-- 添加按钮 -->
    <el-button type="primary" @click="openAddDialog" style="margin-top: 20px">添加学生</el-button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      students: [],
      total: 0,
      pageSize: 5,
      currentPage: 1,
      dialogVisible: false,
      formData: {
        id: null,
        password: '',
        name: '',
        age: 0,
        gender: '',
        email: '',
        group_id: 0
      }
    };
  },
  mounted() {
    this.fetchStudents(this.currentPage);
  },
  methods: {
    async fetchStudents(page) {
      const res = await axios.get(`/api/students/?page=${page}`);
      this.students = res.data.results;
      this.total = res.data.count;
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchStudents(page);
    },
    openAddDialog() {
      this.formData = {
        id: null,
        password: '',
        name: '',
        age: 0,
        gender: '',
        email: '',
        group_id: 0
      };
      this.dialogVisible = true;
    },
    editStudent(student) {
      this.formData = { ...student };
      this.dialogVisible = true;
    },
    async submitStudent() {
      try {
    const data = {
      ...this.formData,
      password: this.formData.password // 确保包含密码
    };
    
    if (this.formData.id) {
      await axios.put(`/api/students/${this.formData.id}/`, data);
    } else {
      await axios.post('/api/students/', data);
    }
    
    this.dialogVisible = false;
    this.fetchStudents(this.currentPage);
  } catch (error) {
    console.error('操作失败:', error);
  }},
    async deleteStudent(id) {
      await axios.delete(`/api/students/${id}/`);
      this.fetchStudents(this.currentPage);
    }
  }
};
</script>

<style scoped>
.student-management {
  padding: 20px;
}
</style>